const {ccclass, property} = cc._decorator;

const mahjongSprites: string[] = [];


@ccclass
export default class MagjongMgr extends cc.Component {

    /**
     * 左边玩家的牌的图集
     */
    @property({
        type: cc.SpriteAtlas,
        displayName: '左边玩家的牌的图集'
    })
    leftAtlas: cc.SpriteAtlas = null;


    /**
     * 右边玩家的牌的图集
     */
    @property({
        type: cc.SpriteAtlas,
        displayName: '右边玩家的牌的图集'
    })
    rightAtlas: cc.SpriteAtlas = null;

    
    /**
     * 下边玩家的牌的图集
     */
    @property({
        type: cc.SpriteAtlas,
        displayName: '下边玩家的牌的图集'
    })
    bottomAtlas: cc.SpriteAtlas = null;


    /**
     * 上边玩家的牌的图集
     */
    @property({
        type: cc.SpriteAtlas,
        displayName: '上边玩家的牌的图集'
    })
    bottomFoldAtlas: cc.SpriteAtlas = null;


    /**
     * 所有牌的背面图集(包括白板)
     */
    @property({
        type: cc.SpriteAtlas,
        displayName: '所有牌的背面图集(包括白板)'
    })
    emptyAtlas: cc.SpriteAtlas = null;


    /**
     * 左右上玩家的背面牌的图片
     */
    @property({
        type: [cc.SpriteFrame],
        displayName: '左右上玩家的背面牌的图片'
    })
    holdsEmpty: cc.SpriteFrame[] = [];


    /**
     * 上下碰杠的预制体
     */
    @property({
        type: cc.Prefab,
        displayName: '上下碰杠的预制体'
    })
    pengPrefabSelf: cc.Prefab = null;


    /**
     * 左右碰杠的预制体
     */
    @property({
        type: cc.Prefab,
        displayName: '左右碰杠的预制体'
    })
    pengPrefabLeft: cc.Prefab = null;


    sides: string[] = null;
    pres: string[] = null;
    foldPres: string[] = null;

    

    
    
    onLoad() {
        
        if(cc.vv == null){
            console.log('MahjongMgr.ts->onLoad加载时，cc.vv没有值', cc.vv.logStyle);
            return;
        }
        cc.vv.directions = {
            myself: 'myself',
            right: 'right',
            up: 'up',
            left: 'left',
        };
        cc.vv.preAtlasString = {
            M: 'M_',
            R: 'R_',
            B: 'B_',
            L: 'L_',
        };
        
        this.sides = [cc.vv.directions.myself, cc.vv.directions.right, cc.vv.directions.up, cc.vv.directions.left];
        this.pres = [cc.vv.preAtlasString.M, cc.vv.preAtlasString.R, cc.vv.preAtlasString.B, cc.vv.preAtlasString.L];
        this.foldPres = ['B_', 'R_', 'B_', 'L_'];
        cc.vv.mahjongmgr = this;

        // 筒
        for(let i = 1; i < 10; i++){
            mahjongSprites.push('dot_' + i);
        }

        // 条
        for(let i = 1; i < 10; i++){
            mahjongSprites.push('bamboo_' + i);
        }

        // 万
        for(let i = 1; i < 10; i++){
            mahjongSprites.push('character_' + i);
        }

        // 中、发、白
        mahjongSprites.push('red');
        mahjongSprites.push('green');
        mahjongSprites.push('white');

        // 东南西北风
        mahjongSprites.push('wind_east');
        mahjongSprites.push('wind_west');
        mahjongSprites.push('wind_south');
        mahjongSprites.push('wind_north');
    }


    /**
     * 获取图片名字的后面一部分
     * @param id 
     */
    getMahjongSpriteByID(id: number){
        return mahjongSprites[id];
    }

    /**
     * 获取牌的种类 
     * @param id 
     * @returns {number} 0->筒 | 1->条 | 2->万
     */
    getMahjongType(id: number){
        if(id >= 0 && id < 9){
            return 0;
        }
        else if(id >= 9 && id < 18){
            return 1;
        }
        else if(id >= 18 && id < 27){
            return 2;
        }
    }

    /**
     * 获取牌的图片
     * @param pre 方位前缀
     * @param mjid 
     */
    getSpriteFrameByMJID(pre: string, mjid: number){
        let spriteFrameName = pre + this.getMahjongSpriteByID(mjid);
        let spriteFrame: cc.SpriteFrame;
        switch(pre){
            case cc.vv.preAtlasString.M:
                spriteFrame = this.bottomAtlas.getSpriteFrame(spriteFrameName);
                break;
            case cc.vv.preAtlasString.B:
                spriteFrame = this.bottomFoldAtlas.getSpriteFrame(spriteFrameName);
                break;
            case cc.vv.preAtlasString.L:
                spriteFrame = this.leftAtlas.getSpriteFrame(spriteFrameName);
                break;
            case cc.vv.preAtlasString.R:
                spriteFrame = this.rightAtlas.getSpriteFrame(spriteFrameName);
                break;
        }
        return spriteFrame;
    }


    /**
     * 根据ID获取牌的音效的路径
     * @param id 
     */
    getAudioURLByMJID(id: number){
        let realId = 0;
        if(id >= 0 && id < 9){
            realId = id + 21;
        }
        else if(id >= 9 && id < 18){
            realId = id - 8;
        }
        else if(id >= 18 && id < 27){
            realId = id - 7;
        }
        return 'nv/' + realId + '.mp3';
    }


    /**
     * 获取某个方位玩家扣着的牌的背面的图片
     * @param side 
     */
    getEmptySpriteFrame(side: string){
        let spriteFrame: cc.SpriteFrame;
        switch(side){
            case cc.vv.directions.up:
                spriteFrame = this.emptyAtlas.getSpriteFrame('e_mj_b_up');
                break;
            case cc.vv.directions.myself:
                spriteFrame = this.emptyAtlas.getSpriteFrame('e_mj_b_bottom');
                break;
            case cc.vv.directions.left:
                spriteFrame = this.emptyAtlas.getSpriteFrame('e_mj_b_left');
                break;
            case cc.vv.directions.right:
                spriteFrame = this.emptyAtlas.getSpriteFrame('e_mj_b_right');
                break;
        }
        return spriteFrame;
    }
    /**
     * 获取某个方位玩家站着的牌的背面的图片
     * @param side 
     */
    getHoldsEmptySpriteFrame(side: string){
        let spriteFrame: cc.SpriteFrame;
        switch(side){
            case cc.vv.directions.up:
                spriteFrame = this.emptyAtlas.getSpriteFrame('e_mj_up');
                break;
            case cc.vv.directions.myself:
                break;
            case cc.vv.directions.left:
                spriteFrame = this.emptyAtlas.getSpriteFrame('e_mj_left');
                break;
            case cc.vv.directions.right:
                spriteFrame = this.emptyAtlas.getSpriteFrame('e_mj_right');
                break;
        }
        return spriteFrame;
    }


    /**
     * 排序,默认筒->条->万的顺序，如果有定缺将定缺的放到最右边
     * @param mahjongs 
     * @param dingque 
     */
    sortMJ(mahjongs: number[], dingque: number){
        mahjongs.sort((a, b) => {
            if(dingque >= 0){
                let t1 = this.getMahjongType(a);
                let t2 = this.getMahjongType(b);
                if(t1 != t2){
                    if(dingque == t1){
                        return 1;
                    }
                    else if(dingque == t2){
                        return -1;
                    }
                }
            }
            return a - b;
        });
    }


    /**
     * 获取方位的字符串
     * @param localIndex 
     */
    getSide(localIndex: number){
        return this.sides[localIndex];
    }

    /**
     * 获取麻将图集的前缀
     * @param localIndex 
     */
    getPre(localIndex){
        return this.pres[localIndex];
    }


    getFoldPre(localIndex){
        return this.foldPres[localIndex];
    }
}
